/**
 * 
 */
package com.ctn.core.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.ctn.base.BaseController;
import com.ctn.core.response.RestResult;
import com.ctn.task.entity.Task;
import com.ctn.task.manage.TaskManage;
import com.ctn.util.Security;

/**
 * @author Zale
 *
 */
@RestController
public class TaskController extends BaseController{
	@Autowired
	private TaskManage taskManage;
	@RequestMapping(value="/task",method=RequestMethod.POST)
	public Object addTask(HttpServletRequest request,Task task){
		RestResult result = new RestResult();
		task.setId(Security.generateUUID());
		taskManage.addNewTask(task);
		result.setCode(HttpStatus.SC_OK);
		result.setMsg("创建任务成功");
		result.setResult(task.getId());
		return result;
	}
	
	@RequestMapping(value="/task",method=RequestMethod.PUT)
	public Object uptTask(HttpServletRequest request,Task task){
		RestResult result = new RestResult();
		if(!taskManage.getWaitingTasks().contains(task)){
			result.setCode(HttpStatus.SC_BAD_REQUEST);
			result.setMsg("不存在此任务，或者任务已经执行");
		}
		taskManage.updateTask(task);
		return result;
	}
	
	@RequestMapping(value="/task/{id}",method=RequestMethod.GET)
	public Object getTask(HttpServletRequest request,@PathVariable("id")String id){
		RestResult result = new RestResult();
		return result;
	}
	@RequestMapping(value="/task/{id}",method=RequestMethod.DELETE)
	public Object reomveTask(HttpServletRequest request,@PathVariable("id")String id){
		RestResult result = new RestResult();
		return result;
	}
}
